﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Diagnostics.Contracts;

using Kooboo.Collections;
using System.ComponentModel.Composition;

namespace Kooboo.Commerce.Repositories
{
    [Export(typeof(IShopSettingRepository))]
    public partial class EntityRepositories:IShopSettingRepository
    {
        #region ShopSetting

        //private static readonly string __ShopSettingConfigPath__ = HttpContext.Current.Server.MapPath("~/ShopSetting.Config");

        private volatile object lockobject = new object();
        private static IShopSetting _ShopSetting = null;

        public IShopSetting ShopSetting
        {
            get
            {
                if (_ShopSetting == null)
                {
                    lock (lockobject)
                    {
                        if (_ShopSetting == null)
                        {
                            var item = this.DataReaderContext.ShopSettings.FirstOrDefault();
                            if (item == null)
                            {
                                item = new ShopSetting()
                                {
                                    CurrencyISOCode = "USD",
                                    PriceIndex = 0,
                                    SizeUnit = "CM",
                                    WeightUnit = "KG",
                                    ThreadCulture = "en",
                                    EnableStock = false,
                                    ThumbnailHeight = 100,
                                    ThumbnailWidth = 100,
                                    DefaultDeliveryDays = 0,
                                };

                            }

                            _ShopSetting = item;
                        }
                    }
                }

                return _ShopSetting;
            }
        }



        public void Put(IShopSetting shopSetting)
        {
            Contract.Requires(shopSetting != null);

            if (shopSetting == null)
            {
                throw new ArgumentNullException("Put(IShopSetting shopSetting)");
            }


            var item = this.DataWriterContext.ShopSettings.FirstOrDefault();
            if (item == null)
            {
                item = new ShopSetting();

                this.DataWriterContext.ShopSettings.InsertOnSubmit(item);
            }

            item.CurrencyISOCode = shopSetting.CurrencyISOCode;
            item.PriceIndex = shopSetting.PriceIndex;
            item.SizeUnit = shopSetting.SizeUnit;
            item.ThreadCulture = shopSetting.ThreadCulture;
            item.WeightUnit = shopSetting.WeightUnit;
            item.EnableStock = shopSetting.EnableStock;
            item.DefaultDeliveryDays = shopSetting.DefaultDeliveryDays;

            var thumnailSize = this.DataWriterContext.ImageSizes.Where(i => i.Name == "Thumnail" && i.IsCustom == false).FirstOrDefault();

            if (thumnailSize == null)
            {
                thumnailSize = new ImageSize()
                {
                    IsCustom = false,
                    Name = "Thumnail"
                };

                this.DataWriterContext.ImageSizes.InsertOnSubmit(thumnailSize);
            }
            thumnailSize.Height = shopSetting.ImageSettings.Thumbnail.Height;
            thumnailSize.Width = shopSetting.ImageSettings.Thumbnail.Width;



            var detailSize = this.DataWriterContext.ImageSizes.Where(i => i.Name == "Detail" && i.IsCustom == false).FirstOrDefault();
            if (detailSize == null)
            {
                detailSize = new ImageSize()
                {
                    IsCustom = false,
                    Name = "Detail"
                };
                this.DataWriterContext.ImageSizes.InsertOnSubmit(detailSize);
            }
            detailSize.Height = shopSetting.ImageSettings.Detail.Height;
            detailSize.Width = shopSetting.ImageSettings.Detail.Width;


            var cartSize = this.DataWriterContext.ImageSizes.Where(i => i.Name == "Cart" && i.IsCustom == false).FirstOrDefault();
            if (cartSize == null)
            {
                cartSize = new ImageSize()
                {
                    IsCustom = false,
                    Name = "Cart"
                };

                this.DataWriterContext.ImageSizes.InsertOnSubmit(cartSize);
            }
            cartSize.Height = shopSetting.ImageSettings.Cart.Height;
            cartSize.Width = shopSetting.ImageSettings.Cart.Width;


            var listSize = this.DataWriterContext.ImageSizes.Where(i => i.Name == "List" && i.IsCustom == false).FirstOrDefault();
            if (listSize == null)
            {
                listSize = new ImageSize
                {
                    IsCustom = false,
                    Name = "List"
                };

                this.DataWriterContext.ImageSizes.InsertOnSubmit(listSize);
            }

            listSize.Height = shopSetting.ImageSettings.List.Height;
            listSize.Width = shopSetting.ImageSettings.List.Width;



            var customSizes = this.DataWriterContext.ImageSizes.Where(i => i.IsCustom).ToList();

            var result = customSizes.Compare(shopSetting.ImageSettings.CustomSizes, (m, n) => m.Id == n.Id, (m, n) =>
                {
                    m.Name = n.Name;
                    m.Height = n.Height;
                    m.Width = n.Width;
                });


            foreach (var i in result.Added)
            {
                var imagesize = new ImageSize()
                {
                    Name = i.Name,
                    Height = i.Height,
                    Width = i.Width,
                    IsCustom = true
                };

                this.DataWriterContext.ImageSizes.InsertOnSubmit(imagesize);
            }


            foreach (var i in result.Deleted)
            {
                this.DataWriterContext.ImageSizes.DeleteOnSubmit(i as ImageSize);
            }

            this.DataWriterContext.SubmitChanges();

            //_ShopSetting = shopSetting;
            //after update, need to reload Shop setting
            _ShopSetting = null;
        }


        #endregion
    }
}
